package blancosMikel.controller.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import blancosMikel.controller.beans.directorioBeans;
import blancosMikel.controller.beans.informativaBeans;
import blancosMikel.controller.beans.noticiasBeans;
import blancosMikel.controller.beans.usuariosBeans;
import blancosMikel.model.dao.directorioDAO;
import blancosMikel.model.dao.informativaDAO;
import blancosMikel.model.dao.noticiasDAO;

/**
 * Servlet implementation class sistemConfig
 */
@WebServlet(description = "configura el modulo informativo", urlPatterns = { "/configSistema" })
public class sistemConfig extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public sistemConfig() {
        super();
        System.out.println("SVR.ConfigSistema----------> Ejecutandose!");
    }

    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.usuarios----------> doGet");
		response.sendRedirect("blancosMikel");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("SVR.usuarios----------> doPost");
		HttpSession sesion  = request.getSession();
		
		//beans
		informativaBeans infoBean = new informativaBeans();
		directorioBeans dirBean = new directorioBeans();
		noticiasBeans notBean = new noticiasBeans();
		usuariosBeans bean = new usuariosBeans();
		
		//daos
		informativaDAO infoDao = new informativaDAO();
		directorioDAO dirDao = new directorioDAO();
		noticiasDAO notDao = new noticiasDAO();

		String mapping = null;
		int dispatch = Integer.parseInt(request.getParameter("dispatch"));
		int id_buscado=0;
		System.out.println("SVR.usuarios.doPost----------> Cargando el disparador");

		if(request.getParameter("id") != null){
			id_buscado = Integer.parseInt(request.getParameter("id"));
		}
		try {
			switch (dispatch) {
			case 1: //Actualizacion del modulo informativo
				bean = (usuariosBeans) sesion.getAttribute("sesion");
				infoBean.setNombre(request.getParameter("empresa"));
				infoBean.setDireccion(request.getParameter("direccion"));
				infoBean.setMail(request.getParameter("email"));
				infoBean.setBienvenida(request.getParameter("bienvenida"));
				infoBean.setSlogan(request.getParameter("slogan"));
				infoBean.setCopy(request.getParameter("copy"));
				if(bean.getSecreta().equals(request.getParameter("secreta"))){
					infoDao.updateInformativa(infoBean);
					request.setAttribute("mensaje","Modulo informativo actualizado");
					infoDao.selectInfo(infoBean);
					request.setAttribute("infoBean",infoBean);
					request.setAttribute("agenda", dirDao.loadPhones());
					request.setAttribute("catNoticias", notDao.selectNoticias());
					mapping="SEGURIDAD/sistema/sistema.jsp";
				}else{
					request.setAttribute("mensaje","Contraseña incorrecta modulo informativo no actualizado");
					infoDao.selectInfo(infoBean);
					request.setAttribute("infoBean",infoBean);
					request.setAttribute("agenda", dirDao.loadPhones());
					request.setAttribute("catNoticias", notDao.selectNoticias());
					mapping="SEGURIDAD/sistema/sistema.jsp";
				}
				break;
			case 2://agregar numeros telefonicos
				dirBean.setId_directorio(dirDao.nextId());
				dirBean.setNumero(request.getParameter("telefono"));
				if(dirBean.getNumero().equals("0")){
					request.setAttribute("mensaje","Error de tipo numerico favor de ingresar solo numeros");
				}else{
					dirDao.insertDirectorio(dirBean);
					request.setAttribute("mensaje","Numero telefonico agregado a la agenda");
				}				
				infoDao.selectInfo(infoBean);
				request.setAttribute("infoBean",infoBean);
				request.setAttribute("agenda", dirDao.loadPhones());
				request.setAttribute("catNoticias", notDao.selectNoticias());
				mapping="SEGURIDAD/sistema/sistema.jsp";
				break;
			case 3://eliminar numeros telefonicos
				dirBean.setId_directorio(id_buscado);
				System.out.println(dirBean.getId_directorio());
				dirDao.deleteDirectorio(dirBean);
				request.setAttribute("mensaje","Numero borrado de la agenda");
				infoDao.selectInfo(infoBean);
				request.setAttribute("infoBean",infoBean);
				request.setAttribute("agenda", dirDao.loadPhones());
				request.setAttribute("catNoticias", notDao.selectNoticias());
				mapping="SEGURIDAD/sistema/sistema.jsp";
				break;
			case 4://agregar noticias
				notBean.setId_noticia(notDao.nextId());
				notBean.setNoticia(request.getParameter("noticia"));
				notDao.insertNoticia(notBean);
				request.setAttribute("mensaje","Noticia agregada!");
				infoDao.selectInfo(infoBean);
				request.setAttribute("infoBean",infoBean);
				request.setAttribute("agenda", dirDao.loadPhones());
				request.setAttribute("catNoticias", notDao.selectNoticias());
				mapping="SEGURIDAD/sistema/sistema.jsp";
				break;
			case 5://eliminar noticias
				notBean.setId_noticia(id_buscado);
				notDao.deleteNoticias(notBean);
				request.setAttribute("mensaje","Noticia eliminada!");
				infoDao.selectInfo(infoBean);
				request.setAttribute("infoBean",infoBean);
				request.setAttribute("agenda", dirDao.loadPhones());
				request.setAttribute("catNoticias", notDao.selectNoticias());
				mapping="SEGURIDAD/sistema/sistema.jsp";
				break;
			}

		} catch (Exception error) {
			System.out.println("ChekSesion.doPost---->" + error.getMessage());
			request.setAttribute("mensaje", error.getMessage());
		}finally{
			request.getRequestDispatcher(mapping).forward(request, response);
		}
	}
}
